<?php
include 'InfoToDatabase.class.php';
$APPID = "wx763167db8165c6d0";
$APPSECRET = "360b6c1227041ad97fa30ea0fc51fb23";

$code = isset($_GET['code']) ? htmlspecialchars($_GET['code']) : '';
//获取OPENID
$get_taken_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx763167db8165c6d0&secret=360b6c1227041ad97fa30ea0fc51fb23&code=" .$code."&grant_type=authorization_code";
//var_dump($get_taken_url);


// 创建一个cURL资源
$ch = curl_init();

// 设置URL和相应的选项
curl_setopt($ch, CURLOPT_URL, $get_taken_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
// 抓取URL并把它传递给浏览器
$res =  curl_exec($ch);

// 关闭cURL资源，并且释放系统资源
curl_close($ch);

$user_obj = json_decode($res,true);

//print_r($res);

//var_dump($user_obj); 


$open_id = $user_obj["openid"];

//$open_id = "os9qxt2j7FF7Ws3QSBRprMa8RORo";
//var_dump($open_id);
$dbinfo = new InfoToDatabase();
$member = $dbinfo->queryById($open_id);
var_dump($member);
if($member == null){
	setcookie("openid",$open_id,time()+7200);
	header("Location:register.html");
	
	
}else {

	setcookie("openid",$open_id,time()+7200);
	header("Location:freeNewDish.html");



}

?>